<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    (function () {
        function Observer() {
            this.obj = {}
            this.on = function (txt, func) {
                if (this.obj[txt]) {
                    this.obj[txt] = this.obj[txt]
                } else {
                    this.obj[txt] = []
                }
                this.obj[txt].push(func)
            }
            this.release = function (txt, msg) {
                if (this.obj[txt]) {
                    this.obj[txt].forEach((item) => {
                        item(msg)
                    })
                }
            }
        }
        window.Observer = Observer
    })()

    var os = new Observer()
    // 订阅消息
    os.on('张三',function(msg){
        console.log('小红收到了来自张三的消息：'+msg);
    }) 
    os.on('张三',function(msg){
        console.log('小黑收到了来自张三的消息：'+msg);
    }) 
    os.on('小玉',function(msg){
        console.log('秀琴收到了来自小玉的消息：'+msg);
    }) 

    // console.log(os)
    // 发布消息
    os.release('张三','晚上八点开播')
    os.release('小玉','今天不直播了')
</script>